
//include standard libraries
#include <sstream>
#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h> 
#include <fstream>
#include <vector>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>

//include gsl stuff
#include <gsl/gsl_rng.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_histogram.h>
#include <gsl/gsl_histogram2d.h>

using namespace std;


//include header

void check_tables();
void slug_progress(float ctten);
void load_table(double ** flux_matrix, double ** low_matrix);

void setsfh(double **sfh_time,double **sfh_cumul,int *npntsfh);

double get_tab_indexes(vector<double> table_input, double new_mass);
double get_tab_indexes_low(vector<double> table_input, double new_mass);

double get_star_index(vector<double> table_input, double new_mass,
	long ntime_axis, double tinx_interp, double last);

double ranpowerlaw(double ymin, double ymax, double gamma, gsl_rng * rand);

double rangauss(double mzero, double sigma, double mmin, double mmax,gsl_rng * rand);

double cluster_draw(gsl_rng * rand);

double kroupa_imf(gsl_rng *rand);

double slope_imf(gsl_rng *rand);

double cluster_populate(double cluster_mass, double evolve_limitmass,
			vector<double> *star_masses, vector<double> *star_indexes,
			gsl_rng * rand, double *kcount, double *biggest_star,
			double ** low_matrix);
double star_draw(double cluster_mass, double evolve_limitmass,
			vector<double> *star_masses, vector<double> *star_indexes,
			gsl_rng * rand, double *kcount, double *biggest_star,
			double ** low_matrix);

//double interpolate_imf_nuv(double time_ind, double mass_ind, 
//			   long size_x);

//double interpolate_imf_fuv(double time_ind, double mass_ind,
//                           long size_x);
//double interpolate_imf_bol(double time_ind, double mass_ind,
//                           long size_x);
//double interpolate_imf_qh0(double time_ind, double mass_ind,
//                           long size_x);
double interpolate_imf_phot(double time_ind, double mass_ind,
                           long size_x, double ** flux_matrix, long matrix_index);

double interpolate_iso_phot(double time_ind, double mass_ind, double mass_ind2,
                           long size_x, double ** flux_matrix, long matrix_index);
	 
void  histo_initialise(gsl_histogram2d * hisNUV,gsl_histogram2d * hisFUV,gsl_histogram2d * hisBOL,
		       gsl_histogram2d * hisHA,gsl_histogram2d * hisNUVfuv,gsl_histogram2d * hisNUVbol,
		       gsl_histogram2d * hisFUVbol,gsl_histogram2d * hisHAbol,gsl_histogram2d * hisHAnuv,
		       gsl_histogram2d * hisHAfuv,gsl_histogram * hisMASS,double * timearray, long ntime, int binhist);

void print_histo(ofstream *outhisto, gsl_histogram * histo);
void print_histo2d(ofstream *outhisto, gsl_histogram2d * histo);
void parse_param(int argc, char *argv[]);
void generate_param();
void imf_initialise();
void hist4d(double **** histogram, long time, double x, double y,
        double * xmin, double * xmax, double * xbin, double * ymin,
        double * ymax,double * ybin, long nhist);

string stringconversion(string stringin, int flag);


//include codes
#include "check_tables.cpp"
#include "load_table.cpp"
#include "ranpowerlaw.cpp"
#include "rangauss.cpp"
#include "cluster_draw.cpp"
#include "cluster_populate.cpp"
#include "star_draw.cpp"
#include "kroupa_imf.cpp"
#include "salpeter_imf.cpp"
#include "chabrier_imf.cpp"
#include "igimf_imf.cpp"
#include "slope_imf.cpp"
#include "get_tab_indexes.cpp"
#include "get_tab_indexes_low.cpp"
#include "get_star_index.cpp"
//#include "interpolate_imf_nuv.cpp"
//#include "interpolate_imf_fuv.cpp"
//#include "interpolate_imf_bol.cpp"
//#include "interpolate_imf_qh0.cpp"
//#include "interpolate_imf_phot.cpp"
#include "interpolate_iso_phot.cpp"
#include "histo_initialise.cpp"
#include "print_histo.cpp"
#include "print_histo2d.cpp"
#include "parse_param.cpp"
#include "generate_param.cpp"
#include "imf_initialise.cpp"
#include "setsfh.cpp"
#include "hist4d.cpp"
#include "slug_progress.cpp"
#include "stringconversion.cpp"

